package aggregation;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;

import com.github.dabai.aggregation.Aggregation;
import com.github.dabai.aggregation.AggregationField;
import com.github.dabai.aggregation.AggregationFieldResult;
import com.github.dabai.aggregation.GroupByKey;
import com.github.dabai.aggregation.IAggregation;
import com.github.dabai.aggregation.IAggregationPost;
import com.google.common.collect.Lists;

import aggregation.pojo.ProfitInput;
import aggregation.pojo.ProfitOut;
import cn.hutool.core.util.RandomUtil;

public class ProfitAggregation {

	static Aggregation<ProfitInput, ProfitOut> profit = new Aggregation<ProfitInput, ProfitOut>()
			//.groupBy(new String[] { "complete_time", "payDate" }, new String[] { "channel", "channel" })
			.addGroupBy("complete_time", "payDate","yyyy-MM-dd")
			.addGroupBy("channel", "channel")
			.outClass(ProfitOut.class).addAggregationField("nfPassgerCount", 0, new IAggregation<ProfitInput>() {
				@Override
				public void aggregation(ProfitInput input, AggregationFieldResult out) {
					// 自愿
					if (input.getApplyType() == 1) {
						out.setResult(out.<Integer>covert() + (Integer) input.getRelatedPassengers());
					}

				}
			}).addAggregationField("yfPassgerCount", 0, new IAggregation<ProfitInput>() {
				@Override
				public void aggregation(ProfitInput input, AggregationFieldResult out) {
					// 自愿
					if (input.getApplyType() == 2) {
						out.setResult(out.<Integer>covert() + (Integer) input.getRelatedPassengers());
					}
				}
			}).addAggregationField("passgerCount", 0, new IAggregation<ProfitInput>() {
				@Override
				public void aggregation(ProfitInput input, AggregationFieldResult out) {
					out.setResult(out.<Integer>covert() + (Integer) input.getRelatedPassengers());
				}
			}).addAggregationField("totalProfit", new BigDecimal(0), new IAggregation<ProfitInput>() {
				@Override
				public void aggregation(ProfitInput input, AggregationFieldResult out) {
					out.setResult(out.<BigDecimal>covert().add(input.getProfit()));
				}
			}).addPostAggregationField("avgProfit", new BigDecimal(0), new IAggregationPost<ProfitInput>() {
				@Override
				public  Object post(GroupByKey key, Map<String, AggregationField<ProfitInput>> aggregations) {
                    int passgerCount = (int) aggregations.get("passgerCount").getResult();
                    BigDecimal totalMoney = (BigDecimal) aggregations.get("totalProfit").getResult();
                    return totalMoney.divide(new BigDecimal(passgerCount), 2, BigDecimal.ROUND_HALF_UP);
				}
            });

	public static void main(String args[]) throws Exception {
		Date now = new Date();
		List<ProfitInput> inputs = Lists.newArrayList();
		RandomUtil.randomEle(Lists.newArrayList("zhixng", "elong", "khj"));
		int count = 200;
		for (int i = 0; i < count; i++) {
			Date complete_time = RandomUtil.randomEle(Lists.newArrayList(DateUtils.addDays(now, -1), DateUtils.addDays(now, -2),DateUtils.addDays(now, -3)));
			String channel = RandomUtil.randomEle(Lists.newArrayList("zhixng", "elong", "khj"));
			Integer applyType = RandomUtil.randomEle(Lists.newArrayList(1, 2));
			Integer relatedPassengers = RandomUtil.randomEle(Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9));
			BigDecimal profit = RandomUtil.randomEle(Lists.newArrayList(new BigDecimal("2"), new BigDecimal("3"), new BigDecimal("0")));
			ProfitInput input = new ProfitInput(complete_time, channel, applyType, relatedPassengers, profit);
			inputs.add(input);
		}
		List<ProfitOut> outs = profit.aggregating(inputs);
		for (ProfitOut out : outs) {
			System.out.println(out);
		}
	}

}
